package com.example.battle.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.example.battle.R;
import com.example.battle.entity.User;
import com.example.battle.util.ActivityCollector;
import com.example.battle.util.MD5;
import com.example.battle.util.ToastUtils;
import com.example.battle.widget.TitleLayout;

public class ChangePwd extends AppCompatActivity {
    private TitleLayout tl_title;
    private EditText edit_pwd1,edit_pwd2;
    private User user;
    private ToastUtils toastUtils = new ToastUtils();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);

        tl_title = (TitleLayout) findViewById(R.id.change_pwd);
        edit_pwd1 = (EditText) findViewById(R.id.et_edit_pwd1);
        edit_pwd2 = (EditText) findViewById(R.id.et_edit_pwd2);

        Intent intent = getIntent();
        user = (User) intent.getSerializableExtra("user");
        //设置监听器
        tl_title.getTextView_forward().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pwd1 = edit_pwd1.getText().toString();
                String pwd2 = edit_pwd2.getText().toString();
                if (pwd1.equals(pwd2)){
                    pwd1 = MD5.md5(pwd1);
                    user.setPassword(pwd1);
                    Login.getInstance().getAppDB().battleDao().updateUserInfo(user);
                    toastUtils.showShort(ChangePwd.this,"修改成功");
                    Intent intent = new Intent(ChangePwd.this,Login.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    ActivityCollector.finishAll();
                }else {
                    toastUtils.showShort(ChangePwd.this,"两次输入密码不一致");
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
